# added by yinchao, Wed June 26 11:21	2024
# todo...
# 需要知道 urlopen 的返回值究竟有哪些方法, 

import re # regular expression
import urllib.request

address = input('Enter an address(default http://data.pr4e.org/cover3.jpg) ')
if address == '':
    address = 'http://data.pr4e.org/cover3.jpg'

try:
    hostname, arg = re.search('http[s]?://(.+?)/(.+)', address).groups()
except Exception as e:
    print(f'The format of address is error, {e}')
    exit()

print(hostname, arg)

html = urllib.request.urlopen(address).read()

print(len(html))

content = html
end_pos = 1000 if len(content) > 1000 else len(content)
first_1000_characters = content[:end_pos]
print(first_1000_characters)
